package com.lemon.components.ant.docmaker;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.Data;
import lombok.experimental.UtilityClass;

@UtilityClass
public class DocConverter {
	
	Pattern pattern = Pattern.compile("(\\w+)\\((\\d+)\\).*",Pattern.CASE_INSENSITIVE);
	
	public DocType typeConvert(String sourceType) {
		if(sourceType!=null)sourceType=sourceType.trim();
		DocType docType = new DocType();
		Matcher matcher = pattern.matcher(sourceType);
		if (matcher.find()) {
			docType.setContainLength(true);
			docType.setTypeLength(Integer.parseInt(matcher.group(2)));
			sourceType=matcher.group(1);
		}
		
		switch (sourceType.toLowerCase()) {
			case "set":
			case "array":
			case "list<string>":
				docType.setTypeName("java.util.List<?>");
				docType.setCollection(true);
				return docType;
			case "long":
			case "n":
				docType.setTypeName("Long");
				docType.setNumber(true);
				return docType;
			case "double":
			case "bigdecimal":
				docType.setTypeName("Double");
				docType.setNumber(true);
				return docType;
			case "integer(int64)":
			case "integer(int32)":
			case "integer":
				docType.setTypeName("Integer");
				docType.setNumber(true);
				return docType;
			default:
				docType.setTypeName("String");
				return docType;
		}
	}
	
	public String printDBType(String sourceType,Integer length) {
		switch (sourceType.toLowerCase()) {
			case "long":
			case "n":
				return "bigint";
			case "double":
				return "double";
			case "bigdecimal":
				return "decimal";
			case "integer(int64)":
			case "integer(int32)":
			case "integer":
				return "int";
			default:
				return "varchar("+(length==null?255:length)+")";
		}
	}
	
	
	@Data
	public static class DocType {
		private String typeName;
		private Integer typeLength;
		private boolean containLength = false;
		private boolean number;
		private boolean collection;
	}
}
